package openc.client;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.util.HashMap;
import java.util.Vector;
import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import openc.model.CatalogueManager;
import openc.ui.JWindowsListBar;

public class MainFrame extends JFrame implements ActionListener {
    public MainFrame() { 
        super("OpenC Client");

        cm = new CatalogueManager();

        setWindowsLookAndFeel();
        setSize(300, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        //Разворачиваем окно на весь экран
        setExtendedState(JFrame.MAXIMIZED_BOTH);

        initializeMenu();

        desktop = new JDesktopPane();
        setLayout(new BorderLayout());
        add(this.desktop, BorderLayout.CENTER);
        JWindowsListBar windowsBar = new JWindowsListBar(desktop);
        add(windowsBar, BorderLayout.SOUTH);
        
        desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
        add(desktop);

        setVisible(true);
    }

    public void actionPerformed(ActionEvent e) {
        String name = e.getActionCommand();
        Vector<String> headings = new Vector<String>(2);
        headings.add("Код");
        headings.add("Наименование");
        Vector<Vector<Object>> data = cm.getElements(name);

        JInternalFrame frame = new JInternalFrame(name, true, true, true, true);
        JTable table = new JTable(data, headings);
        JScrollPane scroll = new JScrollPane(table);
        setVisible(true);

        desktop.add(frame);
        frame.setSize(200, 100);
        frame.setLocation(80, 100);
        frame.add(scroll);
        frame.setVisible(true);
    }

    private void setWindowsLookAndFeel() {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }
        catch (ClassNotFoundException ex) {
            System.err.println(ex.getMessage());
        }
        catch (InstantiationException ex) {
            System.err.println(ex.getMessage());
        }
        catch (IllegalAccessException ex) {
            System.err.println(ex.getMessage());
        }
        catch (UnsupportedLookAndFeelException ex) {
            System.err.println(ex.getMessage());
        }
        SwingUtilities.updateComponentTreeUI(this);
    }

    private void initializeMenu() {
        JMenuBar menuBar = new JMenuBar();
        JMenu catalogues = new JMenu("Справочники");
        HashMap<String, String> map = cm.getCatalogues();
        for (String catalogue : map.keySet()) {
            JMenuItem item = new JMenuItem(map.get(catalogue));
            item.addActionListener(this);
            item.setActionCommand(catalogue);
            catalogues.add(item);
        }
        menuBar.add(catalogues);
        setJMenuBar(menuBar);
    }

    private JDesktopPane desktop;
    private CatalogueManager cm;
}
